package com.hui.forecastapp.city_manager;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.hui.forecastapp.R;
import com.hui.forecastapp.db.DBManager;

import java.util.ArrayList;
import java.util.List;

public class DeleteCityActivity extends AppCompatActivity implements View.OnClickListener {

    ImageView errorIv, rightIv;
    ListView deleteLv;
    // 只是省份+城市
//    List<String> mDatasProvince;
    List<String> mDatas;
    // 将被删除的城市的信息
    List<String> deleteCitys;
//    List<String> deleteProvince;

    private DelectCityAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete_city);

        errorIv = findViewById(R.id.delete_iv_error);
        rightIv = findViewById(R.id.delete_iv_right);
        deleteLv = findViewById(R.id.delete_lv);
//        mDatasProvince = new ArrayList<>();
        mDatas = new ArrayList<>();
        deleteCitys = new ArrayList<>();
//        deleteProvince = new ArrayList<>();

        errorIv.setOnClickListener(this);
        rightIv.setOnClickListener(this);
        // 设置适配器
//        adapter = new DelectCityAdapter(this, mDatasProvince, mDatas, deleteProvince, deleteCitys);
        adapter = new DelectCityAdapter(this, mDatas, deleteCitys);
        deleteLv.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
//        List<String> listProvince = DBManager.queryAllCityName();
//        mDatasProvince.clear();
//        mDatasProvince.addAll(listProvince);

        List<String> list = DBManager.queryAllCityName();
        mDatas.clear();
        mDatas.addAll(list);

        adapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.delete_iv_error:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("提示信息").setMessage("您确定要放弃更改吗？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // 关闭当前activity
                                finish();
                            }
                        }).setNegativeButton("取消", null);
                builder.create().show();
                break;
            case R.id.delete_iv_right:
                // 完成删除操作
                for (int i = 0; i < deleteCitys.size(); i++) {
//                    String province = deleteProvince.get(i);
                    String city = deleteCitys.get(i);
                    DBManager.deleteInfoByCity(city);
                }
                // 删除完成后返回上级页面，需要提醒一级页面也跟随改变
                finish();
                break;
        }
    }
}